/**
 * create by hp
 * date 2020/3/7 12:30
 * @type {string[]}
 */

import * as process from 'child_process'
import {ExecException} from "child_process";

const log = require('single-line-log').stdout;

export async function execDos(dosCommand: Array<String>, logOpen: Boolean = true): Promise<String> {
  if (dosCommand.length == 0) throw new Error("dosCommand is not be zero")
  const command: string = dosCommand.join(" &&")
  return new Promise<String>((resolve, reject) => {
    process.exec(command, {
      maxBuffer: 1024*1024*1
    },(error: ExecException | null, stdout: string, stderr: string) => {
      return resolve()
    }).stdout.on("data", data => {
      if (logOpen) {
        let dataStr = data.toString()
        dataStr = dataStr.replace(/\\r+/g, '\r')
        dataStr = dataStr.replace(/\\n/g, '\n')
        console.log(dataStr)
      }
    })
  })
}
